/**
 * @author mapeng
 * @version 2017-05-12
 */

(function(doc, win) {

	var pageVariables = {"bankDistrict" : "" , "mainBank" : ""};

	$(doc).on("pageInit", "#weixinBranchBankSearchPage", function(e, pageId, $page, isBack) {
		init4Page();
	});
	bindEvents4Page();

	//页面初始化
	function init4Page(){
		var params = CommonUtil.takeOutParamObject() || {};
		pageVariables.bankDistrict = params.bankDistrict;
		pageVariables.mainBank = params.mainBank;
	}

	//页面事件绑定
	function bindEvents4Page(){

		//名称搜索
		$(document).on("input","#weixinBranchBankSearchPage input[name='branchBankName']",function(e){
			var $this = $(this);
			var branchBankName = $this.val();

			if(!branchBankName){
				$("#weixinBranchBankSearchPage #weixinBranchBankSearchContainer").empty();
				return;
			}

			CommonUtil.sendJsonpRequest({
			// weixinBranchBankSearchTest.testSearchBranchBank({
				"url" : App.serverUrl+"/app/wxAccount/getBranchBanks.do",
				"params" : {"province" : pageVariables.bankDistrict.split(",")[0], "city" : pageVariables.bankDistrict.split(",")[1] , "parentId" : pageVariables.mainBank ,  "bankName" : branchBankName},
				"success" : function(result){
					if(result.success.toString() == "true"){//success
						if(result.resultObject && result.resultObject.length > 0){
							CommonUtil.render($("#weixinBranchBankSearchPage #weixinBranchBankSearchContainer"), "weixinbranchBankSearchTPL",{"data":result.resultObject},false);
						}else{
							CommonUtil.render($("#weixinBranchBankSearchPage #weixinBranchBankSearchContainer"), "weixinBranchBankSearch4NoDataTPL",{},false);
						}
					}else{//business error
						DialogUtil.dialogWarn(result.msg);
					}
				},
				"error" : function(){//not 200
					DialogUtil.dialogWarn("请求失败，请稍候重试");
				}
			});

		});

		//清空
		$(document).on("click","#weixinBranchBankSearchPage .search-wrap .zhk-list_gb",function(e){
			$("#weixinBranchBankSearchPage input[name='branchBankName']").val("");
			$("#weixinBranchBankSearchPage #weixinBranchBankSearchContainer").empty();
		});

		//选中支行
		$(document).on("click","#weixinBranchBankSearchPage #weixinBranchBankSearchContainer li",function(e){
			var $this = $(this);
			var contactLine = $this.attr("code");
			var branchBankName = $this.text();

			var cache = CommonUtil.takeOutParamObject() || {};
			cache.contactLine = contactLine;
			cache.branchBankName = branchBankName;
			cache.operation = "branchSearch";
			CommonUtil.injectParamObject(cache);
			$.router.back();
		});
		
	}



	/**
	 * 测试接口
	 * @type {Object}
	 */
	var weixinBranchBankSearchTest = {
		"testSearchBranchBank" : function(options){//支行搜索
			options.success({
				"success" : true,
				"resultObject" : [
					{"bankName" : "上地支行" , "contactLine" : "001"},
					{"bankName" : "西二旗支行" , 'contactLine' : "002"},
					{"bankName" : "沙河支行" , "contactLine" : "003"}
				]
			});
		},
	};

})(document, window);